package br.com.cadastro.persistencia.dao;


import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;

import br.com.cadastro.entidade.Disciplina;
import br.com.cadastro.excecao.DadosException;
import br.com.cadastro.hibernate.util.HibernateUtil;

public class DisciplinaDAO implements Serializable{

	private static final long serialVersionUID = 1L;
	private SessionFactory factory;
	
	public DisciplinaDAO() {
		factory = HibernateUtil.getSessionFactory();
	}     

	@SuppressWarnings("unchecked")
	public List<Disciplina> listarTodos(){
		Session session = factory.openSession();
		session.beginTransaction();
		List<Disciplina> result = session.createQuery("from Disciplina").list();
		session.getTransaction().commit();
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Disciplina> listarPorColuna(String coluna, boolean isAscendente){
			Session session = factory.openSession();
			session.beginTransaction();
			Order order = null;
			if(isAscendente){
				order = Order.asc(coluna);
			}else{
				order = Order.desc(coluna);
			}
			List<Disciplina>result = session.createCriteria(Disciplina.class)
											.addOrder(order).list();
			session.getTransaction().commit();
			session.close();
		
		return result;
	}

	public void salvarOuAlterar(Disciplina disciplina) {
		Session session = factory.openSession();
		session.saveOrUpdate(disciplina);
		session.flush();
		session.close();
	}
	
	public void inserir(Disciplina disciplina){
		Session session = factory.openSession();
		session.save(disciplina);
		session.flush();
		session.close();
	}

	public void alterar(Disciplina us){
		Session session = factory.openSession();
		session.update(us);
		session.flush();
		session.close();
	}
	public void excluir(Disciplina us){
		Session session = factory.openSession();
		session.delete(us);
		session.flush();
		session.close();
	}

	

}